
if (typeof jQuery === 'undefined') {

    throw new Error('CommonUtils.js must need Jquery plugin!!');

}

!(function () {

    var obj = {

        /**
         * 判断（对象、字符串）是否为空
         * @param value         @需要判断的对象或字符串
         * @returns {boolean}
         */
        isEmpty: function (value) {

            if(typeof(value) == 'undefined') return true;

            if(value == '') return true;

            if(value == null) return true;

            return false;

        },

        /**
         * 获取url参数
         * @param key           @url参数
         * @returns {null}      @若有该参数则返回该参数值,相反则返回null
         * @constructor
         */
        urlParam: function (key) {

            var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");

            var r = window.location.search.substr(1).match(reg);

            if (r != null) return decodeURI(r[2]);

            return null;

        },

        /**
         * 获取cookie
         * @param name          @cookie参数名
         * @returns {null}      @若有该参数则返回该参数值,相反则返回null
         */
        getCookie: function (name) {

            var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

            if (arr = document.cookie.match(reg)) {

                return unescape(arr[2]);

            } else {

                return null;

            }

        },

        /**
         * 设置全局cookie
         * @param name          @cookie参数名
         * @param value         @cookie参数值
         * @param expireDays    @cookie过期时间
         */
        setCookie: function (name, val, expireDays) {

            var date=new Date();

            date.setDate(date.getDate()+expireDays);

            document.cookie = name+ "=" +escape(val)+ ((expireDays==null) ? "" : ";path=/;expires="+date.toGMTString());

        },

        delCookie: function (name) {

            var exp = new Date();

            exp.setTime(exp.getTime() - 1);

            var cval= this.getCookie(name);

            if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();

        },

        /**
         * 设置本地缓存数据
         * @param StorageKey    @设置本地缓存key
         * @param StorageVal    @设置本地缓存值
         */
        setLocalStorage: function(StorageKey, StorageVal) {

            localStorage.setItem(StorageKey, StorageVal);

        },

        /**
         * 获取本地缓存数据
         * @param StorageKey    @获取本地缓存的key
         */
        getLocalStorage: function(StorageKey) {

            return localStorage.getItem(StorageKey);

        },

        /**
         * 搜索字符串是否存在表情包 若有则替换为表情
         * @param str          @需要搜索的字符串
         * @return {*}
         */
        replace_em: function (str){

            str = str.replace(/\</g,'&lt;');

            str = str.replace(/\>/g,'&gt;');

            str = str.replace(/\n/g,'<br/>');

            str = str.replace(/\[em_([0-9]*)\]/g,'<img src="/public/res/images/community/writeComment/emoji/qq/$1.gif" border="0" />');

            return str;

        },

        /**
         * 拼接链接地址参数
         * @param url  地址
         * @param option 参数
         * @returns {*}
         */
         joinUrl: function(url, option){

            var result = url;

            if (result.indexOf('?') !== -1){

                // 存在 ?
                result += '&'+option;

            } else {

                result += '?'+option;

            }

            return result;

        },

        formatDate: function (date, formatStr) {
            var timestamp = Date.parse(date);
            var time = new Date(timestamp);
            Date.prototype.format = function (format) {
                var date = {
                    "M+": this.getMonth() + 1,
                    "d+": this.getDate(),
                    "h+": this.getHours(),
                    "m+": this.getMinutes(),
                    "s+": this.getSeconds(),
                    "q+": Math.floor((this.getMonth() + 3) / 3),
                    "S+": this.getMilliseconds()
                };
                if (/(y+)/i.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                for (var k in date) {
                    if (new RegExp("(" + k + ")").test(format)) {
                        format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                    }
                }
                return format;
            };
            return time.format(formatStr);      // 格式化时间，    20171110
        },


        /**
         * 导出excel
         * @param url   请求地址
         * @param data  请求数据
         */
        outExcel: function (url, data) {
            // 点击导出报表显示对应Dialog弹窗
            var that = this;

            $.messager.confirm('提示框','确定要导出报表吗？',function(r){
                if (!r) return;
                var params = that.getParamsFromObject(data);

                window.open(url + "?" + params);
            });
        },

        /**
         * 通过对象获取请求参数
         * @param data
         * @returns {string}
         */
        getParamsFromObject: function (data) {
            var params = [];
            for(var key in data) {
                var value = data[key];
                if (key === 'startTime' || key === 'endTime' || key === 'time') value = this.formatDate(value, 'yyyyMMdd');         // 格式化时间
                params.push( key + '=' + value);
            }
            return  params.join('&');;
        }

    };

    window.CommonUtils = obj;

})(jQuery);


